Ext.ns("Ext.lams.teacher");

/**
 * 教师信息表单
 * 
 * @class Ext.lams.teacher.teacherInfoForm
 * @extends Ext.form.FormPanel
 */
Ext.lams.teacher.teacherInfoForm = Ext.extend(Ext.form.FormPanel, {
			constructor : function(_cfg) {
				Ext.apply(this, _cfg);
				Ext.lams.teacher.teacherInfoForm.superclass.constructor.call(
						this, {
							layout : 'form',
							frame : true,
							defaults : {
								xtype : 'textfield',
								anchor : '95%',
								labelWidth : 65
							},
							items : [{
										hidden : true,
										name : 'TEACHER_ID'
									}, {
										fieldLabel : '教师姓名',
										allowBlank : false,
										blankText : '请填写教师姓名',
										name : 'TEACHER_NAME'
									}, {
										fieldLabel : '学校',
										xtype : "combo_school",
										hiddenName : "SCHOOL_ID"
									}, {
										xtype : "datefield",
										fieldLabel : "出生年月",
										name : 'BIRTH_DATE',
										value : new Date(),
										editable : false,
										format : "Y-m-d"
									}, {
										fieldLabel : '身份证号',
										allowBlank : true,
										blankText : '请填写身份证号',
										name : 'IDENTITYID'
									}, {
										xtype : "combo_sex",
										hiddenName : 'GENDER',

										blankText : "请选择性别"
									}, {
										xtype : 'combo_scode',
										fieldLabel : '级别',
										code_cate : 'TEACHER_LEVEL',
										hiddenName : "TEACHER_LEVEL"
									}, {
										xtype : 'combo_scode',
										fieldLabel : '学历',
										code_cate : 'EDUCATION',
										hiddenName : "EDUCATION"
									}, {
										xtype : 'combo_scode',
										fieldLabel : '婚姻状况',
										code_cate : 'MARRIAGE',
										hiddenName : "MARRIAGE"
									}, {
										xtype : 'combo_scode',
										fieldLabel : '政治面貌',
										code_cate : 'POLI_LAND',
										hiddenName : "POLI_LAND"
									}, {
										fieldLabel : '联系电话',
										allowBlank : true,
										blankText : '请填写联系电话',
										name : 'CONTACT_PHONE'
									}, {
										fieldLabel : '家庭住址',
										allowBlank : true,
										blankText : '请填写家庭住址',
										name : 'ADDRESS'
									}, {
										xtype : 'textarea',
										fieldLabel : '备注',
										name : 'REMARK'
									}]

						})
			},
			initComponent : function() {
				Ext.lams.teacher.teacherInfoForm.superclass.initComponent
						.call(this);
			},
			setValue : function(_r) {
				this.getForm().loadRecord(_r);
			},
			getValue : function() {
				if (this.getForm().isValid())
					return new Ext.data.Record(this.getForm().getValues());
				else
					throw Error("表单填写出错");
			},
			clear : function() {
				this.getForm().reset();
			},
			submit : function(fn) {
				if (this.getForm().isValid()) {
					this.getForm().submit({
								waitMsg : "正在保存数据...",
								url : "addOrUpdateTeacher.action",
								method : "POST",
								success : function(form, action) {
									fn(form, action);
								},
								faild : function(form, action) {
									fn(form, action);
								}
							});
				}
			}
		});

/**
 * 教师信息窗体
 * 
 * @class Ext.lams.teacher.infoWin
 * @extends Ext.Window
 */
Ext.lams.teacher.infoWin = Ext.extend(Ext.Window, {
			// form : {},
			constructor : function(_cfg) {
				Ext.apply(this, _cfg || {});
				Ext.lams.teacher.infoWin.superclass.constructor.call(this, {
							closeAction : "hide"
						});
				this.addEvents("submit");
			},
			initComponent : function() {
				var form = new Ext.lams.teacher.teacherInfoForm();
				this.form = form;
				Ext.apply(this, {
							items : [form],
							buttons : [{
										xtype : 'btn_save',
										handler : this.saveData,
										scope : this
									}, {
										xtype : 'btn_cancel',
										handler : this.cancel,
										scope : this
									}]
						});
				Ext.lams.teacher.infoWin.superclass.initComponent.call(this);
			},
			saveData : function(btn) {
				var me = this;

				try {
					// _r = this.getForm().getValue();
					if (this.getForm().isValid()) {
						btn.disable();
						this.getForm().submit(function(from, action) {
									btn.enable();
									if (action.result["success"]) {
										me.fireEvent("submit", this, action);
									} else {
										H.Msg.error(action.result["message"]);
									}
								});
					}
				} catch (_e) {
					// Ext.MessageBox.alert("ERROR", _e.message);
					H.Msg.error(_e.message);
				}

			},
			getForm : function() {
				return this.form;
			},
			cancel : function() {
				this.getForm().clear();
				this.hide();
			}
		});

/**
 * 查询条件表单
 * 
 * @class Ext.lams.teacher.queryForm
 * @extends Ext.form.FormPanel
 */
Ext.lams.teacher.queryForm = Ext.extend(Ext.form.FormPanel, {
	grid : null,
	constructor : function(_cfg) {
		Ext.apply(this, _cfg || {});
		Ext.lams.teacher.queryForm.superclass.constructor.call(this, {
			layout : 'form',
			items : [{
						layout : 'hbox',
						// anchor : '100%',
						baseCls : 'x-plain',
						layoutConfig : {
							padding : 5
						},
						defaults : {
							margins : '0 15 0 0'
						},
						items : [{
									layout : 'form',
									labelAlign : 'right',
									labelWidth : 35,
									items : [{
												fieldLabel : '学校',
												xtype : "combo_school",
												hiddenName : "SCHOOL_ID"
											}]

								}, {
									layout : 'form',
									labelAlign : 'right',
									labelWidth : 60,
									items : [{
												xtype : "xauto",
												fieldLabel : '教师姓名',
												hiddenName : "TEACHER_NAME",
												fields : [{
															name : "TEACHER_ID"
														}, {
															name : "TEACHER_NAME"
														}],
												valueField : "TEACHER_NAME",
												displayField : "TEACHER_NAME",
												params : {
													sqlCode : 'SEL_TEACHER001'
												},
												emptyText : "输入教师姓名"
											}]

								}/*
									 * , { xtype : 'btn_query', handler :
									 * this.onQuery(this.grid), scope : this }
									 */]
					}]

		})
	},
	getValue : function() {
		if (this.getForm().isValid())
			return new Ext.data.Record(this.getForm().getValues());
		else
			throw Error("表单填写出错");
	}
})
/**
 * 教师管理查询Grid
 * 
 * @class Ext.lams.teacher.managerGrid
 * @extends Ext.grid.GridPanel
 */
Ext.lams.teacher.managerGrid = Ext.extend(Ext.grid.GridPanel, {
			constructor : function(_cfg) {
				Ext.apply(this, _cfg || {});
				Ext.lams.teacher.managerGrid.superclass.constructor.call(this,
						{
							frame : true,
							loadMask : {
								msg : '正在载入数据,请稍等...'
							}
						});
			},
			initComponent : function() {
				var _sm = new Ext.grid.RowSelectionModel({
							singleSelect : true
						});
				var _cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
						{
							header : "教师姓名",
							width : 100,
							dataIndex : "TEACHER_NAME"
						}, {
							header : '所在学校',
							width : 150,
							dataIndex : "SCHOOL_NAME"
						}, {
							header : '出生年月',
							width : 100,
							dataIndex : "BIRTH_DATE"
						}, {
							header : '性别',
							width : 100,
							dataIndex : "GENDER_NAME"
						}, {
							header : "级别",
							width : 50,
							dataIndex : "TEACHER_LEVEL_NAME"
						}, {
							header : "学历",
							width : 50,
							dataIndex : "EDUCATION_NAME"
						}, {
							header : "地址",
							width : 200,
							dataIndex : "ADDRESS"
						}]);
				var _store = new Ext.data.JsonStore({
							proxy : new Ext.data.HttpProxy({
										url : "getTeachers.action",
										method : "GET"
									}),
							totalProperty : "totalCount",
							root : "data",
							fields : ['TEACHER_ID', 'TEACHER_NAME',
									'SCHOOL_ID', 'SCHOOL_NAME', 'BIRTH_DATE',
									'GENDER', 'GENDER_NAME', 'IDENTITYID',
									'TEACHER_LEVEL', 'TEACHER_LEVEL_NAME',
									'CONTACT_PHONE', 'ADDRESS', 'EDUCATION',
									'EDUCATION_NAME', 'MARRIAGE', 'POLI_LAND',
									'PHOTO', 'REMARK']
						});
				var bbar = new Ext.lams.comp.PagingToolbar({
							store : _store
						});
				Ext.apply(this, {
							store : _store,
							sm : _sm,
							cm : _cm,
							bbar : bbar
						});
				Ext.lams.teacher.managerGrid.superclass.initComponent
						.call(this);
			},
			loadData : function(_r, fn) {
				var ds = this.getStore();
				ds.baseParams = _r.data;
				ds.load({
							params : {
								start : 0,
								limit : this.getBottomToolbar().pageSize
							},
							callback : function() {
								fn();
							}
						});
			},
			getSelectedItem : function() {
				var _sel = this.getSelectionModel();
				if (_sel.getCount() == 0)
					throw new Error("未选中某条记录");
				return _sel.getSelected();
			}
		})
/**
 * 主界面
 * 
 * @class Ext.lams.TeacherManagerPanel
 * @extends Ext.form.FormPanel
 */
Ext.lams.TeacherManagerPanel = Ext.extend(Ext.Panel, {
	constructor : function(config) {
		Ext.apply(this, config || {});
		Ext.lams.TeacherManagerPanel.superclass.constructor.call(this, {
					frame : true
				});
	},
	initComponent : function() {
		var center = new Ext.lams.teacher.managerGrid({
					region : 'center'
				});
		this.grid = center;
		var top = new Ext.lams.teacher.queryForm({
					region : 'north',
					height : 40,
					grid : center,
					split : true,
					frame : true,
					collapseMode : 'mini'
				});
		this.qform = top;
		var me = this;
		Ext.apply(this, {
					layout : 'border',
					items : [top, center],
					buttonAlign : 'center',
					buttons : [{
								xtype : 'btn_query',
								handler : this.onQuery,
								scope : this
							}, {
								xtype : 'btn_add',
								handler : this.onAddWin,
								scope : this
							}, {
								xtype : 'btn_edit',
								handler : this.onEditWin,
								scope : this
							}]
				});
		this.createAddWin();
		this.createEditWin();
		Ext.lams.TeacherManagerPanel.superclass.initComponent.call(this);
	},
	createAddWin : function() {
		var me = this;
		if (!this.addWin) {
			var addWin = new Ext.lams.teacher.infoWin({
						title : '新建教师',
						width : '400',
						listeners : {
							submit : function(win, action) {
								Ext.Msg.alert("INFO", action.result["message"]);
								var r = this.getForm().getValue();
								me.grid.getStore().add(r);
								this.getForm().clear();
								this.hide();
							}
						}
					});
			this.addWin = addWin;
		}
	},
	createEditWin : function() {
		var me = this;
		if (!this.editWin) {
			var editWin = new Ext.lams.teacher.infoWin({
						title : '教师编辑',
						width : '400',
						listeners : {
							submit : function(win, action) {
								// Ext.Msg.alert("INFO",
								// action.result["message"]);
								H.Msg.info(action.result["message"],
										function() {
											try {
												var _r = this.getForm()
														.getValue();
												var _sr = me.grid
														.getSelectedItem();
												var _data = _sr.data;
												for (var _i in _data) {
													_sr.set(_i, _r.get(_i));
												}
												_sr.commit();
												me.editWin.hide();
											} catch (_e) {
												// Ext.MessageBox.alert("ERROR",
												// _e.message);
												H.Msg.error(_e.message);
											}

										});

							}
						}
					});
			this.editWin = editWin;
		}
	},
	onQuery : function(btn) {
		try {
			btn.disable();
			var _r = this.qform.getValue();
			this.grid.loadData(_r, function() {
						btn.enable();
					});
		} catch (_e) {
			// Ext.Msg.alert("ERROR", _e.message);
			H.Msg.error(_e.message, function() {
						btn.enable();
					});
		}
	},
	onAddWin : function() {
		this.createAddWin();
		this.addWin.show();
	},
	onEditWin : function() {
		this.createEditWin();
		try {
			var sel = this.grid.getSelectedItem();
			this.editWin.show();
			this.editWin.getForm().setValue(sel);
		} catch (_error) {
			// Ext.MessageBox.alert("ERROR", _error.message);
			H.Msg.error(_error.message)
		}

	}
})